<?php
class ProductsModel extends Model {
	private $table1 = 'products';
	private $table2 = 'province';
	private $table3 = 'city';
	private $table8 = 'area';
	private $table4 = 'products_to_categories';
	private $table5 = 'categories';
	private $table6 = 'categories_description';
	private $table7 = 'order';
	private $talbe8 = 'auction';
	
	//视图
	private $table9 = 'productsdetail';
	/**
	 * 按照指定条件获取商品列表
	 * @param array $where 条件  string $order 排序  string $limit
	 * @return array
	 */
	public function getProductsList($where,$order,$limit){
		/* $products = M($this->table1)->table($this->table1." p")->join($this->table8." ar ON p.area_id=ar.code")->join($this->table3.' c ON p.citycode=c.code')->join($this->table2." prov ON p.provcode=prov.code")->where($where)->order($order)
		->field('p.products_id,p.products_name,p.products_image,p.uid,p.contact_tel,p.contact_person,p.viewd,p.up_flag,p.jiaji_flag,date(p.products_date_added) as products_date_added,p.products_price,p.provcode,p.citycode,p.area_id,prov.name as prov_name,c.name as city_name,ar.name as area_name')->limit($limit)->select();
		 */
		$products = M($this->table9)->where($where)->order($order)->limit($limit)->select();
		//var_dump($products);die;
		//echo M($this->table9)->getLastSql();die;
		foreach($products as $key=>$val){//获取发布商品者成交量
			$scount = M($this->table7)->where(array('publish_uid'=>$val['uid'],'order_status_id'=>7))->count();
			//echo $val['uid']."dd".$scount."<br/>";
			$products[$key]['showcount']=$scount;
		}
		//die;
		return $products;
	}
	
	/**
	 * 获取商品详细信息
	 * @param array $where 
	 */
	public function getProductsDetail($where){
		/* return M($this->table1)->table($this->table1." p")->join($this->table8." ar ON p.area_id=ar.code")->join($this->table3.' c ON p.citycode=c.code')->join($this->table2." prov ON p.provcode=prov.code")->where($where)
		->field('p.products_id,p.products_name,p.products_description,p.products_model,p.products_oldprice,p.products_price,p.products_type,p.products_paimai_price,date(p.products_date_available) as products_date_available,products_weight,
				p.contact_person,p.contact_tel,p.contact_qq,p.contact_wechat,p.contact_address,p.viewd,p.up_flag,p.jiaji_flag,p.products_date_added,
				p.provcode,p.citycode,p.area_id,p.uid,p.products_status,p.products_close,prov.name as prov_name,c.name as city_name,ar.name as area_name')->find(); */
		return M($this->table9)->where($where)->find();
	}
	
	/**
	 * 获取会员发布的商品
	 * @param array $where 条件 string $order 排序 
	 */
	public function getMproductsList($where,$order){
		$prolist =  M($this->table1)->table($this->table1." p")->where($where)->field('p.products_id,p.products_name,p.products_image,p.up_flag,p.jiaji_flag,p.products_date_added,p.products_status,p.show_flag,p.products_price,p.products_type,p.viewd')->select();
		foreach($prolist as $key=>$val){
			$catelist = M($this->table4." ptc")->join($this->table5." c ON ptc.categories_id = c.categories_id")->join($this->table6." cd ON ptc.categories_id=cd.categories_id")
			->where(array('ptc.products_id'=>$val['products_id'],'cd.language_id'=>L('lan_id'),'c.categories_status'=>1))->field('c.categories_id,cd.categories_name')->select();
			$prolist[$key]['products_date_added'] = strtotime($val['products_date_added']);
			$prolist[$key]['cate']=$catelist;
			if($val['products_type']==2){
				$auction = M($this->table8)->where(array('products_id'=>$val['products_id']))->field('price')->order('auction_id desc')->limit(1);
				$prodlist[$key]['auction_price']=$auction['price'];
			}
		}
		return $prolist;
	}
	
	/**
	 * 获取商品基本信息
	 * @param array $where 条件 string $field 获取的字段
	 */
	public function getProductsBasic($where,$field){
		return M($this->table1)->where($where)->field($field)->find();
	}
	
	/**
	 * 根据cate_id 获取商品id数组
	 * @param string $cate_id
	 */
	public function getPidList($cate_id){
		return  M($this->table4)->where(array('categories_id'=>$cate_id))->field('products_id')->select();
		//echo M($this->table4)->getLastSql();die;
	}

	/**
	 * 按照天数获取产品个数
	 * @param string $where
	 */
	public function getPcByday($where=1,$limit,$order){
		if($limit){
			$sql = "select DATE_FORMAT(products_date_added,'%Y-%m-%d') day, count(*) c from ".$this->table1." WHERE ".$where." group by DATE_FORMAT(products_date_added,'%Y%m%d') ORDER BY ".$order." LIMIT ".$limit;
		}else{
			$sql = "select DATE_FORMAT(products_date_added,'%Y-%m-%d') day, count(*) c from ".$this->table1." WHERE ".$where." group by DATE_FORMAT(products_date_added,'%Y%m%d') ORDER BY ".$order;
		}
		//echo $sql;die;
		return M($this->table1)->query($sql);
	}

	
	/**
	 * 获取按天数获取产品的个数
	 * @param string  $where
	 */
	public function getPc($where=1){
		$sql = "select DATE_FORMAT(products_date_added,'%Y-%m-%d') day, count(*) c from ".$this->table1." WHERE ".$where." group by DATE_FORMAT(products_date_added,'%Y%m%d') ";
		$result = M($this->table1)->query($sql);
		return count($result);
	}
	
	/**
	 * 获取统计商品的详情
	 */
	public function getPcDeatil($where,$limit,$order){
		if($limit){
			$sql = "SELECT products_id, products_name, products_date_added, products_status, products_type, products_close, recommend,show_flag FROM ".$this->table1." WHERE ".$where." ORDER BY ".$order." LIMIT ".$limit;
		}else{
			$sql = "SELECT products_id, products_name, products_date_added, products_status, products_type, products_close, recommend,show_flag FROM ".$this->table1." WHERE ".$where." ORDER BY ".$order;
		}
		//echo $sql;die;
		return M($this->table1)->query($sql);
	}
}